from rest_framework import serializers
from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response

from app01 import models
from rest_framework.pagination import PageNumberPagination


class UserModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.UserInfo
        fields = ["id","username","age","email"]

class Mypageination(PageNumberPagination):
    page_size = 2
    page_size_query_param = "size"
    max_page_size = 100

    def get_paginated_response(self, data):
        """自定义修改返回值"""
        # return Response(OrderedDict([
        #     ('count', self.page.paginator.count),
        #     ('next', self.get_next_link()),
        #     ('previous', self.get_previous_link()),
        #     ('results', data)
        # ]))
        #
        return Response([{
            'count':self.page.paginator.count,
            'results': data,
        }])



class UserView(ModelViewSet):
    queryset = models.UserInfo.objects.all().order_by("id")
    serializer_class =  UserModelSerializer
    pagination_class = Mypageination
